
package com.zzvcom.sms.client;

import com.zzvcom.sms.entity.*;
import com.zzvcom.sms.sign.AppSignature;
import com.zzvcom.sms.sign.DefaultAppSignature;
import com.zzvcom.sms.utils.HttpUtil;
import com.zzvcom.sms.utils.PropUtil;
import org.apache.commons.lang3.RandomStringUtils;

import java.math.BigDecimal;

public class DefaultAppClient implements AppClient {
    private final String companyCode;
    private final String appKey;
    private final String appSecret;
    private static final String address = PropUtil.getProp("address", "");
    private static final String lockUrl;
    private static final String unlockUrl;
    private static final String discardUrl;
    private static final String maintainUrl;

    private static final String pauseUrl;
    private static final String resumeUrl;

    private static final String parkingSyncUrl;


    static {
        lockUrl = address + "/api/v1/sms/moped/lock?";
        unlockUrl = address + "/api/v1/sms/moped/unlock?";
        discardUrl = address + "/api/v1/sms/moped/discard?";
        maintainUrl = address + "/api/v1/sms/moped/maintain?";
        pauseUrl = address + "/api/v1/sms/moped/pause?";
        resumeUrl = address + "/api/v1/sms/moped/resume?";
        parkingSyncUrl = address + "/api/v1/sms/parking/sync?";
    }

    private final AppSignature appSignature = new DefaultAppSignature();


    public DefaultAppClient(String companyCode, String appKey, String appSecret) {
        this.companyCode = companyCode;
        this.appKey = appKey;
        this.appSecret = appSecret;
    }

    public String lockMoped(String licensePlateNumber, String rfidPlateNumber, String vin, String rfidNumber, BigDecimal longitude, BigDecimal latitude) {
        AppLockMopedParam param = new AppLockMopedParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, rfidNumber, longitude, latitude, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(lockUrl, param);
    }

    public String unlockMoped(String licensePlateNumber, String rfidPlateNumber, String vin, BigDecimal longitude, BigDecimal latitude) {
        AppUnlockMopedParam param = new AppUnlockMopedParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, longitude, latitude, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(unlockUrl, param);
    }

    public String discardMoped(String licensePlateNumber, String rfidPlateNumber, String vin) {
        AppDiscardMopedParam param = new AppDiscardMopedParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(discardUrl, param);
    }

    public String maintainMoped(String licensePlateNumber, String rfidPlateNumber, String vin) {
        AppMaintainMopedParam param = new AppMaintainMopedParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(maintainUrl, param);
    }


    @Override
    public String mopedPause(String licensePlateNumber, String rfidPlateNumber, String vin, Integer type, BigDecimal longitude, BigDecimal latitude) {
        AppPauseParam param = new AppPauseParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, type, longitude, latitude, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(pauseUrl, param);
    }

    @Override
    public String mopedResume(String licensePlateNumber, String rfidPlateNumber, String vin, BigDecimal longitude, BigDecimal latitude) {
        AppResumeParam param = new AppResumeParam(this.appKey, this.companyCode, licensePlateNumber, rfidPlateNumber, vin, longitude, latitude, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(resumeUrl, param);
    }

    @Override
    public String parkingSync(Long syncStartTime, Integer pageNumber) {
        AppParkingSyncParam appParkingSyncParam = new AppParkingSyncParam(this.appKey, this.companyCode, syncStartTime, pageNumber, System.currentTimeMillis() / 1000L, RandomStringUtils.randomAlphanumeric(Constants.NONCE_LEN));
        return this.request(parkingSyncUrl, appParkingSyncParam);
    }

    private String request(String url, AppParam param) {
        String signature = this.appSignature.sign(param, this.appSecret);
        return HttpUtil.post(url + HttpUtil.buildQueryString(param.toMap(), "sign", signature));
    }


}
